<?php


namespace App\Http\Controllers;


use App\CodeResponse;
use App\Models\Luck;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class LuckController extends WxController
{

    public function get(Request $request)
    {
        $user_id = $request->user_id;
        $user_tel = $request->user->mobile;
        $openid = $request->user->openid;
        $city = $request['city'];
        $luck_info = Luck::query()->where('user_id',$user_id)->first();
        if ($luck_info) {
                 if (in_array($luck_info->luck_status, [1, 2])) {
                     return $this->success(['mall_id' => $luck_info->mall_id, 'luck_id' => $luck_info->id, 'luck_key' => config('luck.luck_desc')[$luck_info->luck_key]]);
                 }
                 if ($luck_info->luck_status == 4) {
                     $status = $this->reissue($luck_info);
                     if ($status == 1) {
                         return $this->success(['mall_id' => $luck_info->mall_id, 'luck_id' => $luck_info->id, 'luck_key' => config('luck.luck_desc')[$luck_info->luck_key]]);
                     } else {
                         return $this->fail(CodeResponse::FAIL, '补发失败!');
                     }
                 }
             }else{
                 if (time() > strtotime(config('luck.luck_end_time'))) {
                     $luck_key = -5;
                 }else{
                    if ($city == '广州市'){
                        $rand = mt_rand(1,3);
                        $config_info = config('luck.luck_config_gz')[$rand];
                    }elseif($city == '佛山市'){
                        $config_info = config('luck.luck_config_fs');
                    }elseif($city == '武汉市'){
                        $config_info = config('luck.luck_config_wh');
                    }else{
                        $config_info = config('luck.luck_config_sz');
                    }
                    $luck_key = (new Luck())->getLuck($user_id,$config_info);
            }
        }
        //中奖
        if (is_string($luck_key) || $luck_key > 0) {
            $data = [];
            $data['user_id'] = $user_id;
            $data['openid'] = $openid;
            $data['user_tel'] = $user_tel;
            $data['luck_status'] = 1;
            $data['luck_key'] = $luck_key;
            $data['update_time'] = time();
            $data['create_time'] = time();
            $data['luck_desc'] = config('luck.luck_title')[$luck_key];
            $data['mall_id'] = mb_substr($luck_key,0,5);
            $data['type'] = (in_array($luck_key,['10295_7','P0299_11','10164_9','10311_8','10630_4'])) ? 1 : 2;
            $data['partner_trade_no'] = (new Luck())->getTradeNo($user_id);
            $id = Luck::query()->insertGetId($data);
            (new Luck())->incTodaySum($luck_key);
            return $this->success(['mall_id'=>$data['mall_id'],'luck_id'=>$id,'luck_key' =>config('luck.luck_desc')[$luck_key]]);
        }else{
            //没有中奖的伙计
//            $data = [];
//            $data['user_id'] = $user_id;
//            $data['openid'] = $openid;
//            $data['user_tel'] = $user_tel;
//            $data['luck_status'] = 0;
//            $data['luck_key'] = $luck_key;
//            $data['update_time'] = time();
//            $data['create_time'] = time();
//            $data['luck_desc'] = '';
//            $data['mall_id'] = 0;
//            $data['type'] = 1;
////                $data['partner_trade_no'] = (new Luck())->getTradeNo($user_id);
//            $id = Luck::query()->insertGetId($data);
            if($luck_key == -1 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:奖品已派完");
            }
            elseif($luck_key == -2 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:奖品已派完");
            }

            elseif($luck_key == -3 || $luck_key == -301  || $luck_key == -302 || $luck_key == -303 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:抽奖不中");
            }
            elseif($luck_key == -4 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:不能重复中奖");
            }
            elseif($luck_key == -5 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:活动已结束");
            }
            elseif($luck_key == -6 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:中奖互斥");
            }
            elseif($luck_key == -7 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:超过今日限制");
            }
            elseif($luck_key == -8 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:抽奖次数无效");
            }
            elseif($luck_key == -100 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:限制连续中奖");
            }
            elseif($luck_key == -200 )
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:未到开放时间");
            }
            else
            {
                Log::info('info.luck|失败|抽奖|用户ID：'.$user_id.'|'. $luck_key."|原因:其他原因");
            }
            return $this->fail(CodeResponse::AUTH_INVALID_ACCOUNT,'您来迟啦，奖品已经派完');
        }
    }



    private function reissue($luck_info){
        $method = 'fzapi.crm.WXGiftcertIssue';
        $content = [];
        $content['member_telephone'] = $luck_info->user_tel;
        $content['cardid'] = config('luck.luck_code')[$luck_info->luck_key];
        $content['giftcert_batchno'] = config('luck.luck_code')[$luck_info->luck_key];
        $content['source_type'] = 10;
        $content['order_no'] = $luck_info['partner_trade_no'];
        $content['issue_qty'] = 1;
        $content['operator_code'] ='yuegoujie';
        $content['operator_password'] ='yuegoujie0918';
        $time = date('Y-m-d H:i:s');
        if ($luck_info->mall_id=='P0299'){
            $url = config('luck.yhc_url');
            $query = new Requery($method, $content, $time,$url);
            $res = $query->query($time);
        }else{
            $time = date('Y-m-d H:i:s');
            $query = new Requery($method,$content,$time);
            $res = $query->query($time);
        }
        if ($res['code'] == 1000){
            Luck::query()->where(['id' =>$luck_info->id ])->update(['luck_status' => 2,'send_time' =>time(),'api_info'=>'重新补发']);
            return 1;
        }else{
            Luck::query()->where(['id' =>$luck_info->id ])->update(['luck_status' => 4,'api_info' => json_encode($res)]);
            return 0;
        }
    }

    private function is_can_luck($user_id)
    {
        if(in_array($user_id,[4])){
            return 1;//可以抽奖
        }
        //是否已抽奖
        $sum = Luck::query()->where('user_id',$user_id)->whereIn('luck_status',[1,2])->count();
        if ($sum > 0) {
            return -1;
        }
        return 1;//可以抽奖
    }

}